package com.java.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.java.model.*;
import com.java.service.*;
import com.java.utils.CommonUtils;
import com.java.utils.JsonBody;
import com.java.utils.PageList;
import com.java.utils.ResponseUtil;;

@RestController
@RequestMapping("/thing")
public class ThingController {
	private ThingService thingService;

	public ThingService getThingService() {
		return thingService;
	}

	@Autowired
	public void setThingService(ThingService thingService) {
		this.thingService = thingService;
	}
	@Autowired
	private ThingTypeService thingtypeService;
	@Autowired
	private ThingType2Service thingtype2Service;
	@Autowired
	private PingLunService pinglunService;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;

	@CrossOrigin(origins = "*")
	@ResponseBody
	@RequestMapping("getbyid")
	public JsonBody getbyid(int id) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("thingtype", thingtypeService.Get());
		map.put("thingtype2", thingtype2Service.Get());
		Thing thing=thingService.GetByID(id);
		if(id>0)
		{
			thing.setThingtype(thingtypeService.GetByID(thing.getThingtypeId()));
			thing.setThingtype2(thingtype2Service.GetByID(thing.getThingtype2Id()));
		}
		map.put("add", thing);
		return Util.SetData(map);
	}




	@CrossOrigin(origins = "*")
	@ResponseBody
	@RequestMapping("add")
	public JsonBody add(@RequestBody Thing s) {
			int count=0;
			if(s.getId()==0)
				count = thingService.Add(s);
			else
				count=thingService.Edit(s);
			if(count>0)
			return Util.SetMap("操作成功", true);
			else
				return Util.SetMap("操作失败",false);
	}

	@CrossOrigin(origins = "*")
	@ResponseBody
	@RequestMapping("list")
	public JsonBody Get(@RequestBody Thing h) {

			List<Thing> list = thingService.Get(h);
			for(int i=0;i<list.size();i++)
			{
				ThingType tt=thingtypeService.GetByID(list.get(i).getThingtypeId());
				ThingType2 tt2=thingtype2Service.GetByID(list.get(i).getThingtype2Id());
				list.get(i).setThingtype(tt);
				list.get(i).setThingtype2(tt2);
			}
			return Util.SetData(list);
	}


	@CrossOrigin(origins = "*")
	@ResponseBody
	@RequestMapping("del")
	public JsonBody Del(int id) {
		if(thingService.Del(id)>0)
			return Util.SetMap("删除成功", true);
		else
			return Util.SetMap("删除失败", false);
	}


}
